@extends('admin.layouts.content')
@section('content')
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" onsubmit="return false;">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">消息标题</label>
                        <div class="layui-input-block">
                            <input type="text" name="msg_title" id="msg_title" placeholder="请输入类型名称" autocomplete="off" class="layui-input">
                        </div>
                    </div>


                    <div class="layui-inline">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit lay-filter="query-form" style="height: 34px; line-height: 34px;">查询</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <table class="layui-hide" id="data-table" lay-filter="data-table"></table>

{{--    <script type="text/html" id="data-table-bar">--}}
{{--        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>--}}
{{--        <a class="layui-btn layui-btn-xs"  lay-event="delete">删除</a>--}}
{{--    </script>--}}

{{--    <script type="text/html" id="data-table-top-bar" lay-filter="data-table-top-bar">--}}
{{--        <div class="layui-btn-container">--}}
{{--            <button class="layui-btn layui-btn-sm" lay-event="add">添加消息</button>--}}
{{--        </div>--}}
{{--    </script>--}}

@endsection
@section('script')
    <script>


        layui.use(['table', 'jquery', 'form', 'upload'], function(){
            $ = layui.jquery;
            var table = layui.table;
            var form = layui.form;
            var upload = layui.upload;

            var tableIns = table.render({
                elem: '#data-table'

                ,url:'{{url('admin/api/news/newsApiList')}}'
                ,page: true
                ,cellMinWidth: 80
                ,limit: 10
                ,where: { msg_title: '' }
                ,cols: [[
                    {field:'id', width:80, title: 'ID', sort: true}
                    ,{title: '消息类型', minWidth: 100,templet:function (d) {
                            if(d.msg_type_name){
                                return d.msg_type_name.msg_type;
                            }
                            return  '';
                    }}
                    ,{field:'msg_title', title: '消息标题', minWidth: 100}
                    ,{field:'msg_content', title: '消息内容', minWidth: 100}
                    ,{title: '发送人',  minWidth: 100,templet:function (d) {
                            if(d.getSend){
                                return d.getSend.username;
                            }
                            return  '';
                        }}

                    ,{title: '接收人',  minWidth: 100,templet:function (d) {
                            if(d.getRecevier){
                                return d.getRecevier.username;
                            }
                            return  '';
                        }}

                    ,{field:'send_date', title: '发送时间', width: 180}
                    , {field:'read_date', title: '阅读时间', width: 180}
                    , {field:'is_read', title: '状态', width: 180}

                   // ,{title: '操作', toolbar: '#data-table-bar', width: 450, fixed:'right'}
                ]]
                ,toolbar: "#data-table-top-bar"
                ,defaultToolbar: ['filter', 'exports', 'print']
                // ,parseData: function (res) {
                //     return {
                //         "code": 0, //解析接口状态
                //         "msg": "获取成功", //解析提示文本
                //         "count": res.data.total, //解析数据长度
                //         "data": res.data.data //解析数据列表
                //     };
                // }
            });
            form.on('submit(query-form)', function () {
                eventMap.tableReload();
            });

            table.on('tool(data-table)', function(obj){
                switch (obj.event) {
                    case 'delete':
                        eventMap.delete(obj);
                        break;
                    case 'edit':
                        eventMap.edit(obj, form);
                        break;
                    case 'business':
                        eventMap.business(obj);
                        break;
                    case "clone":
                        eventMap.clone(obj);
                        break;
                    case "link":
                        eventMap.link(obj);
                        break;
                    case "publish":
                        eventMap.publish(obj);
                        break;
                    default:
                        layer.msg(obj.event);
                }
            });
            table.on('toolbar(data-table)', function (obj) {
                if (obj.event == 'add') {
                    eventMap.add(obj);
                }
            });

            var eventMap = {
                add: function (form) {
                    layer.open({
                        type: 2,
                        title: "添加消息",
                        content: '{{url('admin/news/create')}}',
                        area: ['100%', '100%']
                    });
                }
                ,edit: function (obj, form) {
                    layer.open({
                        type: 2,
                        title: '编辑-' + obj.data.msg_type,
                        content: '{{url('admin/news/edit')}}/' + obj.data.id,
                        area: ['100%', '100%']
                    });
                }




                ,delete: function (obj) {
                    layer.confirm('您确定要删除此类型吗？', {
                        btn: ['确认', '取消']
                    }, function(index, layero){
                        $.ajax({
                            url : '{{url('admin/api/news/delete')}}',
                            type : "POST",
                            data : {
                                id : obj.data.id,
                                status: 2,
                                _token: "{{ csrf_token() }}"
                            },
                            dataType : 'json',
                            success : function(result) {
                                if(result.error == 0) {
                                    layer.msg(result.msg);
                                    tableIns.reload();
                                }
                                else {
                                    layer.msg(result.msg, { 'icon': 2 });
                                }
                            }
                        });
                    }, function(index){
                        layer.closeAll();
                    });
                }
                ,tableReload: function () {
                    var name = $("#msg_title").val();
                    tableIns.reload({
                        where: {msg_title: name}
                        ,page: {
                            curr: 1
                        }
                    });
                    $("#msg_title").val(name);
                }
            };
        });
    </script>
@endsection
